package com.trainoo.redis.testDemo;

import io.lettuce.core.output.ScoredValueScanOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * <p> add description here </p>
 *
 * @author tao.zhou
 * @version 1.0
 * @date 2020/6/12 16:26
 */
@Component
public class RedisDemo {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void unionAndDiff(){
        redisTemplate.opsForSet().add("k1", "a");
        redisTemplate.opsForSet().add("k1", "b");
        redisTemplate.opsForSet().add("k1", "c");

        redisTemplate.opsForSet().add("k2", "c");
        redisTemplate.opsForSet().add("k2", "d");
        redisTemplate.opsForSet().add("k2", "e");

        redisTemplate.opsForSet().add("k3", Arrays.asList("1","2","3", "4", "5", "6", "7", "a").toArray());


        // 合并两个set
        Set<Object> union = redisTemplate.opsForSet().union("k1", "k2");
        System.out.println("union: " + union);

        // 返回第一个set中有别于后面一个set的值
        Set<Object> difference = redisTemplate.opsForSet().difference("k1", "k2");
        System.out.println("difference: " + difference);

        // 返回第一个set中有别于后面一堆set的值
        Set<Object> difference2 = redisTemplate.opsForSet().difference("k1", Arrays.asList("k2","k3"));
        System.out.println("difference2: " + difference2);

        // 返回两个集合的交集
        Set<Object> intersect = redisTemplate.opsForSet().intersect("k1", "k2");
        System.out.println("intersect: " + intersect);

        // 返回所有元素
        Set<Object> members = redisTemplate.opsForSet().members("k3");
        System.out.println("members: " + members);

        // 返回set中指定个数的元素, 可重复
        List<Object> randomMembers = redisTemplate.opsForSet().randomMembers("k1", 5);
        System.out.println("randomMembers: " + randomMembers);

        // 返回set中指定个数的不重复元素。
        Set<Object> distinctRandomMembers = redisTemplate.opsForSet().distinctRandomMembers("k1", 5);
        System.out.println("distinctRandomMembers: " + distinctRandomMembers);


    }
}
